/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.metastore;

import cn.ac.iie.di.ban.commons.RuntimeEnv;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author ulss
 */
public class MetaStoreManager {

    private static Logger logger = Logger.getLogger(MetaStoreManager.class.getName());
    private static MetaStoreManager metaStoreManager = null;

    public MetaStoreManager() {
    }

    public static MetaStoreManager getMetaStoreManger() {
        if (metaStoreManager == null) {
            try {
                metaStoreManager = new MetaStoreManager();
            } catch (Exception ex) {
                metaStoreManager = null;
                logger.error("initializing metaStoreManager is failed for " + ex, ex);
            }
        }
        return metaStoreManager;
    }

    public Map<String, Load2DBEntity> getLoad2DBRules() throws Exception {
        Map<String, Load2DBEntity> confGetTables = new HashMap<>();
        String mysql_conf = (String) RuntimeEnv.getParam(RuntimeEnv.DB_CONF);
        String[] mysqlConf = mysql_conf.split("[#]");
        for (String confOne : mysqlConf) {//db_conf=se2audit;msg_db.audit|ip|data_exchange_audit_mq|data_exchange_audit_mq|audit|2|10
            String[] confStrings = confOne.split("[;]");
            String entityName = confStrings[0];
            String[] entityCols = confStrings[1].split("[|]");
            if (entityCols.length == 7) {
                String database = entityCols[0].split("[.]")[0];
                String table = entityCols[0].split("[.]")[1];
                logger.info("new mysql entity : " + entityName);
//                (String entityName, String db, String table, String thread, String loadNum, String loadType, String MQName, String group,String ip)
                confGetTables.put(entityName, new Load2DBEntity(entityName, database, table, entityCols[5], entityCols[6], entityCols[4], entityCols[3], entityCols[2],entityCols[1]));
            } else {
                logger.error("please check conf_mysql !!! ");
                System.exit(0);
            }
        }
        return confGetTables;
    }

    /*
    字段映射解析
    get_db_columns=se2record;req_id,req_id:type,type:sql,sql:Status,Status:Detail,Detail:result,result:task_id,task_id:timestamp,timestamp:uniq_seq,uniq_seq
     */
    public Map<String, Map<String, String>> columnsMapperParse(String columnsString) {
        Map<String, Map<String, String>> columnMaps = new HashMap<>();
        if (columnsString == null || "".equals(columnsString)) {
            return columnMaps;
        }
        Map<String, String> columnMap = new HashMap<>();
        String[] columnsStrings = columnsString.split("[#]");
        for (String one : columnsStrings) {
            String[] columnsStringsOne = one.split("[;]");
            String entityName = columnsStringsOne[0];
            String[] columns = columnsStringsOne[1].split("[:]");
            for (String column : columns) {
                String[] colOne = column.split("[,]");
                columnMap.put(colOne[0], colOne[1]);
            }
            columnMaps.put(entityName, columnMap);
        }
        return columnMaps;
    }

}
